package com.gable.report.service.impl;

import com.gable.report.common.util.MonthEnumUtil;
import com.gable.report.dao.IChartsForAreaMaintainerDao;
import com.gable.report.model.ChartsAreaQuery;
import com.gable.report.model.ChartsAssetsMaintainInfoView;
import com.gable.report.model.ChartsForMaintainerView;
import com.gable.report.model.User;
import com.gable.report.service.ChartsForAreaMaintainerService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import static com.gable.report.util.ChartsUtil.getParamMap;


/**
 * Created by lvdouStart on 2017/7/26.
 * 区报表维修商service实现类
 */
@Service
public class ChartsForAreaMaintainerServiceImpl implements ChartsForAreaMaintainerService {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    IChartsForAreaMaintainerDao chartsForAreaMaintainerDao;


    @Override
    public Map<String, Object> queryAllAssetsAndRepairSizes(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        ChartsForMaintainerView allAssets = chartsForAreaMaintainerDao.queryAllAssets(paramMap);
        paramMap.put("asId",1);
        paramMap.put("asName","'大维修'");
        paramMap.put("maintainSize",1);
        ChartsForMaintainerView bigRepair = chartsForAreaMaintainerDao.queryRepairSizes(paramMap);
        paramMap.put("asId",2);
        paramMap.put("asName","'中维修'");
        paramMap.put("maintainSize",2);
        ChartsForMaintainerView  middleRepair = chartsForAreaMaintainerDao.queryRepairSizes(paramMap);
        paramMap.put("asId",3);
        paramMap.put("asName","'小维修'");
        paramMap.put("maintainSize",3);
        ChartsForMaintainerView smallRepair = chartsForAreaMaintainerDao.queryRepairSizes(paramMap);
        returnMap.put("allAssets",allAssets);
        returnMap.put("list", Arrays.asList(bigRepair,middleRepair,smallRepair));
        return returnMap;
    }

    @Override
    public Map<String, Object> queryHospitals(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        ChartsForMaintainerView allAssets = chartsForAreaMaintainerDao.queryAllAssets(paramMap);
        List<ChartsForMaintainerView> list = chartsForAreaMaintainerDao.queryHospitals(paramMap);
        double avgToRepairCounts = 0.0;
        if(allAssets != null && list != null && list.size() != 0){
            avgToRepairCounts = allAssets.getCurTotalRepairCounts()/list.size()*1.0;
        }
        returnMap.put("avgToRepairCounts",avgToRepairCounts);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceModels(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsForMaintainerView> list = chartsForAreaMaintainerDao.queryDeviceModels(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceTypes(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsForMaintainerView> list = chartsForAreaMaintainerDao.querySecondDeviceTypes(paramMap);
        ChartsForMaintainerView allAssets = chartsForAreaMaintainerDao.queryAllAssets(paramMap);
        double avgToRepairCounts = 0.0;
        if(allAssets != null && list != null && list.size() != 0){
            //二级68分类数量之和
            int size = list.stream().map((x)->x.getList() == null ? 1 : x.getList().size()).reduce(0,Integer::sum);
            avgToRepairCounts = allAssets.getCurTotalRepairCounts()/size*1.0;
        }
        returnMap.put("avgToRepairCounts",avgToRepairCounts);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceBrands(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        return null;
    }

    @Override
    public Map<String, Object> queryForMonth(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsForMaintainerView> list = chartsForAreaMaintainerDao.queryForMonth(paramMap);
        for(ChartsForMaintainerView obj : list){
            obj.setName(MonthEnumUtil.getValue(obj.getId()));
        }
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryMaintainers(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsForMaintainerView> list = chartsForAreaMaintainerDao.queryMaintainers(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

    @Override
    public Map<String, Object> queryDeviceInfoByModelName(User user, ChartsAreaQuery chartsAreaQuery) throws Exception {
        Map<String,Object> returnMap = new HashMap<>();
        Map<String,Object> paramMap = getParamMap(user, chartsAreaQuery);
        List<ChartsAssetsMaintainInfoView> list = chartsForAreaMaintainerDao.queryDeviceInfoByModelName(paramMap);
        returnMap.put("list",list);
        return returnMap;
    }

}
